<?php
namespace app\store\controller;

use app\store\model\Merchant as OrderMerchant;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require 'vendor/autoload.php';

class Merchant extends Controller
{
    public function index()
    {
        return $this->getList('全部订单列表', 'all');
    }

    /**
     * 已拆分订单列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function Write_off()
    {
        return $this->getList('已支付 待核销', 'Write_off');
    }

    /**
     * 已取消订单列表
     * @return mixed
     */
    public function cancel(){
        return $this->getList('已取消', 'cancel');
    }

    public function confirm(){
        return $this->getList('已核销 待确认','confirm');
    }

    public function complete(){
        return $this->getList('已评价 待完成','complete');
    }

    public function evaluate(){
        return $this->getList('已确认 待评价','evaluate');
    }

    public function end(){
        return $this->getList('已完成','end');

    }

    /**
     * 订单列表
     * @param $title
     * @param $dataType
     * @return mixed
     * @throws \think\exception\DbException
     */
    private function getList($title, $dataType)
    {
        $model = new OrderMerchant;
        $list = $model->userList($dataType, $this->request->param());
        return $this->fetch('index',  compact('dataType','title', 'list'));
    }

    public function detail($id)
    {
        $model = new OrderMerchant;
        $detail = $model->detail($id);
        return $this->fetch('detail',compact('detail'));
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export($dataType){
        $model = new OrderMerchant;
        $list = $model->userList($dataType, $this->request->param());
        return $this->orderList($list);
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function orderList($list){
        //让程序一直运行
        set_time_limit(0);
        //设置程序运行内存
        ini_set('memory_limit', '700M');

        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        $worksheet->setTitle('商户订单信息');

        $worksheet->setCellValueByColumnAndRow(1, 1, '商户订单信息');
        $worksheet->setCellValueByColumnAndRow(1, 2, '订单号');
        $worksheet->setCellValueByColumnAndRow(2, 2, '交易号');
        $worksheet->setCellValueByColumnAndRow(3, 2, '商品名');
        $worksheet->setCellValueByColumnAndRow(4, 2, '买家');
        $worksheet->setCellValueByColumnAndRow(5, 2, '付款金额');
        $worksheet->setCellValueByColumnAndRow(6, 2, '支付方式');
        $worksheet->setCellValueByColumnAndRow(7, 2, '使用时间');
        $worksheet->setCellValueByColumnAndRow(8, 2, '失效时间');
        $worksheet->setCellValueByColumnAndRow(9, 2, '付款时间');
        $worksheet->setCellValueByColumnAndRow(10,2, '订单状态');

        $worksheet->getColumnDimension('A')->setWidth(25);
        $worksheet->getColumnDimension('B')->setWidth(25);
        $worksheet->getColumnDimension('C')->setWidth(25);
        $worksheet->getColumnDimension('D')->setWidth(25);
        $worksheet->getColumnDimension('E')->setWidth(25);
        $worksheet->getColumnDimension('F')->setWidth(25);
        $worksheet->getColumnDimension('G')->setWidth(25);
        $worksheet->getColumnDimension('H')->setWidth(25);
        $worksheet->getColumnDimension('I')->setWidth(25);
        $worksheet->getColumnDimension('J')->setWidth(25);

        //合并单元格
        $worksheet->mergeCells('A1:J1');
        $styleArray = [
            'font' => [
                'bold' => true
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        //设置单元格样式
        $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);

        $worksheet->getStyle('A2:J2')->applyFromArray($styleArray)->getFont()->setSize(14);
        $len = 3;

        foreach ( $list as $order ){
            $worksheet->setCellValueByColumnAndRow(1,  $len, $this->filterValue($order['order_no']));
            $worksheet->setCellValueByColumnAndRow(2,  $len, $this->filterValue($order['transaction_no']));
            $worksheet->setCellValueByColumnAndRow(3,  $len, $this->filterValue($order['name']));
            $worksheet->setCellValueByColumnAndRow(4,  $len, $this->filterValue($order['nickName']));
            $worksheet->setCellValueByColumnAndRow(5,  $len, $this->filterValue($order['article_price']));
            $worksheet->setCellValueByColumnAndRow(6,  $len, $this->filterValue($order['pay_type'] == 10 ? '支付宝支付' : '微信支付'));
            $worksheet->setCellValueByColumnAndRow(7,  $len, $this->filterTime($order['start_time']));
            $worksheet->setCellValueByColumnAndRow(8,  $len, $this->filterTime($order['end_time']));
            $worksheet->setCellValueByColumnAndRow(9,  $len, $this->filterTime($order['create_time']));
            $worksheet->setCellValueByColumnAndRow(10, $len, $this->filterValue($this->status($order['order_status'])));
            $len++;
        }

        $styleArrayBody = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['argb' => '666666'],
                ],
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        $total_rows = $len + 2;
        //添加所有边框/居中
            $worksheet->getStyle('A1:J'.$total_rows)->applyFromArray($styleArrayBody);

        // 下载
        $filename = '订单信息.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');

    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    /**
     * 日期值过滤
     * @param $value
     * @return string
     */
    private function filterTime($value)
    {
        if (!$value) return '';
        return $this->filterValue(date('Y-m-d H:i:s', $value));
    }

    /**
     * 订单状态
     * @param $order
     * @return mixed|string
     */
    public function status($order){
        if( $order == 200 ){
            $order = '已支付 待核销';
        } else if( $order == 201 ){
            $order = '已取消';
        } else if( $order == 300 ){
            $order = '已核销 待确认';
        } else if( $order == 400 ){
            $order = '已确认 待评价';
        } else if( $order == 500 ){
            $order = '已完成';
        } else if( $order == 600 ){
            $order = '已评价 待完成';
        } else if( $order == 800 ){
            $order = '已结束';
        }
        return $order;
    }
}